﻿/* BDVB-App
Copyright (C) 2014 Sascha Manns <Sascha.Manns@outlook.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


using System.Collections.Generic;
using System.Linq;
using System.Text;
using Windows.UI.Text;
using Windows.UI.Xaml.Controls;

namespace AppStudio.Controls.Html2Xaml
{
    public class CustomGrid : Grid
    {
        public string GetXaml(Dictionary<string, string> TextBlockAttributes)
        {
            StringBuilder sb = new StringBuilder();
          
            sb.Append("<Viewbox StretchDirection=\"DownOnly\"><Grid>");

            sb.Append("<Grid.ColumnDefinitions>");
            foreach (var column in this.ColumnDefinitions)
                sb.Append("<ColumnDefinition/>");
            sb.Append("</Grid.ColumnDefinitions>");

            sb.Append("<Grid.RowDefinitions>");
            foreach (var row in this.RowDefinitions)
                sb.Append("<RowDefinition/>");
            sb.Append("</Grid.RowDefinitions>");

            string tbAttr = string.Empty;
            if (TextBlockAttributes.Count > 0)
                tbAttr = string.Join(string.Empty, TextBlockAttributes.Select(tb => string.Format(" {0}=\"{1}\"", tb.Key, tb.Value)).ToArray());

            List<RowAdjustment> adjustments = new List<RowAdjustment>();
            foreach (var item in this.Children.OfType<TextBlock>())
            {
                int column = Grid.GetColumn(item);
                int row = Grid.GetRow(item);
                int colSpan = Grid.GetColumnSpan(item);
                int rowSpan = Grid.GetRowSpan(item);

                if (adjustments.Count > 0)
                    column += adjustments.Sum(a => a.Row == row && a.StartAt <= column ? a.AdjAmt : 0);

                bool isBold = item.FontWeight.Equals(FontWeights.Bold);
                sb.Append(string.Format("<TextBlock TextTrimming=\"WordEllipsis\" TextWrapping=\"Wrap\"{0}", tbAttr));
                if (column > 0)
                    sb.Append(string.Format(" Grid.Column=\"{0}\" Margin=\"4,0,0,0\"", column));
                if (colSpan > 1)
                    sb.Append(string.Format(" Grid.ColumnSpan=\"{0}\" TextAlignment=\"Center\"", colSpan));
                if (row > 0)
                    sb.Append(string.Format(" Grid.Row=\"{0}\"", row));
                if (rowSpan > 1)
                {
                    for (int xx = 1; xx < rowSpan; xx++)
                    {                       
                        adjustments.Add(new RowAdjustment() { Row = row + xx, StartAt = column, AdjAmt = colSpan });
                    }
                    sb.Append(string.Format(" VerticalAlignment=\"Center\" Grid.RowSpan=\"{0}\"", rowSpan));
                }
                if (isBold)
                    sb.Append(" FontWeight=\"Bold\"");
                sb.Append(string.Format(" Text=\"{0}\" />", item.Text));
            }
            sb.Append("</Grid></Viewbox>");
            return sb.ToString();
        }


        private class RowAdjustment
        {
            public int Row { get; set; }
            public int StartAt { get; set; }
            public int AdjAmt { get; set; }
        }
    }
}
